package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ValidationUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.base.mapper.BaseCompanyMapper;
import com.ruoyi.base.domain.BaseCompany;
import com.ruoyi.base.service.IBaseCompanyService;

/**
 * 企业Service业务层处理
 * 
 * @author yepanpan
 * @date 2020-11-09
 */
@Service
public class BaseCompanyServiceImpl implements IBaseCompanyService 
{
    private static final Logger log = LoggerFactory.getLogger(BaseCompanyServiceImpl.class);
    
    @Autowired
    private BaseCompanyMapper baseCompanyMapper;

    /**
     * 查询企业
     * 
     * @param id 企业ID
     * @return 企业
     */
    @Override
    public BaseCompany selectBaseCompanyById(Long id)
    {
        return baseCompanyMapper.selectBaseCompanyById(id);
    }

    /**
     * 查询企业列表
     * 
     * @param baseCompany 企业
     * @return 企业
     */
    @Override
    public List<BaseCompany> selectBaseCompanyList(BaseCompany baseCompany)
    {
        return baseCompanyMapper.selectBaseCompanyList(baseCompany);
    }

    /**
     * 新增企业
     * 
     * @param baseCompany 企业
     * @return 结果
     */
    @Override
    public int insertBaseCompany(BaseCompany baseCompany)
    {
        baseCompany.setCreateTime(DateUtils.getNowDate());
        return baseCompanyMapper.insertBaseCompany(baseCompany);
    }

    /**
     * 修改企业
     * 
     * @param baseCompany 企业
     * @return 结果
     */
    @Override
    public int updateBaseCompany(BaseCompany baseCompany)
    {
        baseCompany.setUpdateTime(DateUtils.getNowDate());
        return baseCompanyMapper.updateBaseCompany(baseCompany);
    }

    /**
     * 批量删除企业
     * 
     * @param ids 需要删除的企业ID
     * @return 结果
     */
    @Override
    public int deleteBaseCompanyByIds(Long[] ids)
    {
        return baseCompanyMapper.deleteBaseCompanyByIds(ids);
    }

    /**
     * 删除企业信息
     * 
     * @param id 企业ID
     * @return 结果
     */
    @Override
    public int deleteBaseCompanyById(Long id)
    {
        return baseCompanyMapper.deleteBaseCompanyById(id);
    }
    
    /**
     * 修改企业状态
     * 
     * @param ids 需要修改的企业ID
     * @param status 新的状态
     * @return 结果
     */
    @Override
    public int updateBaseCompanyStatus(Long[] ids, String status) {
    	 return baseCompanyMapper.updateBaseCompanyStatus(ids, status);
    }
    
    /**
     * 导入企业数据
     * 
     * @param companyList 企业数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    @Transactional
    public String importCompany(List<BaseCompany> companyList, Boolean isUpdateSupport, String operName) {
    	if (StringUtils.isNull(companyList) || companyList.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BaseCompany vo : companyList)
        {
        	List<String> vs = ValidationUtils.validate(vo);
        	if(vs != null) {
        		failureNum++;
                failureMsg.append("<br/>" + failureNum + "、企业 " + vo.getTitle() + " 数据不完整："+ CollectionUtils.join(vs, ","));
                continue;
        	}
            try
            {                 
            	//查找旧数据
            	BaseCompany cond = new BaseCompany();
            	cond.setTitle(vo.getTitle());
            	BaseCompany old = baseCompanyMapper.findCompany(cond);
                if (StringUtils.isNull(old))
                {
                    this.insertBaseCompany(vo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、课程 " + vo.getTitle() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    vo.setId(old.getId());
                    this.updateBaseCompany(vo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、课程 " + vo.getTitle() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、课程 " + vo.getTitle() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、课程 " + vo.getTitle() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
